package com.friendsoft.crm.bean;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.collections.CollectionUtils;

import com.friendsoft.crm.entity.Address;
import com.friendsoft.crm.entity.Contact;
import com.friendsoft.crm.exception.NoEntityFoundException;

/**
 * 
 * @author huyduong
 */
@Stateless
public class AddressBean extends BaseEntityBean<Address> {
	public Address getAddressById(long id) throws NoEntityFoundException {
		Address address = find(id) ;
		if(address == null) {
			throw new NoEntityFoundException("cannot find address with id " + id) ;
		}
		return address ;
	}
	
	public Address createAddress(Address address) {
		persist(address) ;
		return address ;
	}
	
	public Address createAddress(Contact contact , Address address) {
		if(contact != null && address != null) {
			address.setContact(contact);
		}
		return createAddress(address) ;
	}
	
	public List<Address> createAddressesForContact(Contact contact, List<Address> addresses) {
		if(CollectionUtils.isNotEmpty(addresses)) {
			for(Address address : addresses) {
				createAddress(contact , address) ;
			}
		}
		
		return addresses ;
	}
	
	public List<Address> updateAddressesForContact(Contact contact , List<Address> addresses) {
		List<Address> result = new ArrayList<Address>() ;
		
		return result ;
	}
	
	public Address updateAddress(Address address) throws NoEntityFoundException {
		if(find(address.getId()) == null) {
			throw new NoEntityFoundException("cannot find address with id " + address.getId()) ;
		}
		merge(address) ;
		return address ;
	}
	
	public Address deleteAddress(long id) throws NoEntityFoundException {
		Address address = find(id) ;
		if(address == null) {
			throw new NoEntityFoundException("cannot find address with id " + id) ;
		}
		
		remove(address) ;
		return address ;
	}
}
